//
//  ST_AttractiveView.swift
//  btvideo
//
//  Created by mac on 2019/11/27.
//  Copyright © 2019 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift
import SnapKit
import SwiftyAttributes

//首页展示吸引力-View
class ST_AttractiveView: UIView {
    
    static var colors = [UIColor.init(hexString: "#FC79B7")!,UIColor.init(hexString: "#F85D49")!,UIColor.init(hexString: "#FEA91E")!]
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = Color.white
        self.cornerRadius = 12
        
        self.addSubview(content_label)
        content_label.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.right.equalToSuperview().offset(-40)
            make.top.equalToSuperview().offset(15)
            make.bottom.equalToSuperview().offset(-51)
        }
        
     
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //        MARK: - Properties
    fileprivate lazy var content_label:UILabel = {
        let control = UILabel.init()
//        control.numberOfLines = 0
        return control
    }()
    
    fileprivate lazy var paragraph:NSMutableParagraphStyle = {
       var p = NSMutableParagraphStyle()
        p.lineSpacing = 5
        return p
    }()
   
    fileprivate lazy var tagMaxWidth:CGFloat = {
        return (ScreenWidth - 60 - 10)/3.0
    }()
    
    
    
    fileprivate var tags_btn = [ST_AttractiveTagView]()
    
    
    //    MARK: - private method
    fileprivate func figure_out_over_string_index(text:String)->Int?{
        
        if self.tagWidth(text: text) <= tagMaxWidth{
            
            return nil
            
        }else{
            
            var newText = text as NSString
            var i = 0
            while self.tagWidth(text: newText as String) > tagMaxWidth {
                newText = newText.substring(with: NSRange.init(location: 0, length: newText.length - 1)) as NSString
                i += 1
            }
            return text.count - i
            
        }
        
        
        
    }
    
    fileprivate func tagWidth(text:String)->CGFloat{
        
        return text.width(withConstrainedHeight: 100, font:  UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular)) + 40
    }
    
    fileprivate func create_tag_button(model:ST_MediaInfoTagModel,color:UIColor){
        
        let btn = ST_AttractiveTagView()
        
        btn.cover_image_view.pin_setImage(from: model.icon.compress_image().url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
        
        btn.backgroundColor = color
        var text = model.value
        
        if let length = figure_out_over_string_index(text: text){
            
            text = (text as NSString).substring(with: NSRange.init(location: 0, length: length))
        }
        
        btn.content_label.text = text
        
        let btnWidth = self.tagWidth(text: text)
        
        self.addSubview(btn)
        
        
        btn.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-17)
            make.width.equalTo(btnWidth)
            make.height.equalTo(24)
            if let last = tags_btn.last{
                make.left.equalTo(last.snp.right).offset(5)
            }else{
                make.left.equalToSuperview().offset(15)
            }
        }
        
        tags_btn.append(btn)
    }
    
    
    
}


extension ST_AttractiveView{
    
//    MARK: - Bind Model
    
    func bind(text:String,tags:Array<ST_MediaInfoTagModel>)->CGFloat{
        
        for btn in tags_btn{
            btn.removeFromSuperview()
        }
        tags_btn.removeAll()
        
        for (idx,item) in tags.enumerated() {
            create_tag_button(model: item, color: ST_AttractiveView.colors[idx])
        }
        
        let attributedText =  text.withAttributes([Attribute.paragraphStyle(self.paragraph),Attribute.textColor(UIColor.init(hex: 0x333333)!),Attribute.font(UIFont.systemFont(ofSize: 14))])
        
        content_label.attributedText = attributedText
        
        let textHeight = attributedText.height(withConstrainedWidth: ScreenWidth-55-30)
        
        return textHeight + 51 + 15
    }
    
    
    
}


//MARK: -
class ST_AttractiveTagView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(cover_image_view)
        cover_image_view.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(12)
            make.width.height.equalTo(14)
        }
        
        self.addSubview(content_label)
        content_label.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalTo(cover_image_view.snp.right).offset(5)
        }
        
        self.cornerRadius = 12
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
//    MARK: - Properties
    lazy var cover_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
        return control
    }()
    
    lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = UIColor.white
        control.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular)
        return control
    }()
}
